package com.hong.demo.springboot.es.controller;

import com.hong.demo.springboot.es.entity.es.EsBlog;
import com.hong.demo.springboot.es.entity.mysql.MySQLBlog;
import com.hong.demo.springboot.es.repository.es.EsBlogRepository;
import com.hong.demo.springboot.es.repository.mysql.MySQLBlogRepository;
import lombok.Data;
import org.apache.lucene.queryparser.xml.builders.BooleanQueryBuilder;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Optional;

@RestController // 返回JSON给前端
public class DataController {

    @Autowired
    MySQLBlogRepository mySQLBlogRepository;

    @Autowired
    EsBlogRepository esBlogRepository;

    @GetMapping("/blogs")
    public Object blog(){
        List<MySQLBlog> blogs = mySQLBlogRepository.queryAll();
        return blogs;
    }

    @PostMapping("/search")
    public Object search(@RequestBody Param param){
        HashMap<String, Object> map = new HashMap<>();
        StopWatch watch = new StopWatch();
        watch.start();
        String type = param.getType();
        if(type.equalsIgnoreCase("mysql")){
            //mysql
            List<MySQLBlog> mySQLBlogs = mySQLBlogRepository.queryBlogs(param.keyword);
            map.put("list", mySQLBlogs);
        } else if (type.equalsIgnoreCase("es")) {
            //es
            /**
             * POST /blog/_search
             * {
             *   "query": {
             *     "bool": {
             *       "should": [
             *         {
             *           "match_phrase": {
             *             "title":"springboot"
             *           }
             *         },{
             *           "match_phrase": {
             *             "content":"springboot"
             *           }
             *         }
             *       ]
             *     }
             *   }
             * }
             */
            BoolQueryBuilder builder = QueryBuilders.boolQuery();
            builder.should(QueryBuilders.matchPhraseQuery("title", param.getKeyword()));
            builder.should(QueryBuilders.matchPhraseQuery("content", param.getKeyword()));
            String queryString = builder.toString();
            System.out.println(queryString);
            Page<EsBlog> search = (Page<EsBlog>) esBlogRepository.search(builder);
            List<EsBlog> content = search.getContent();
            map.put("list", content);
        } else {
            return "I don't understand";
        }
        watch.stop();
        long timeMillis = watch.getTotalTimeMillis();
        map.put("duration",timeMillis);
        return map;
    }

    @GetMapping("/blog/{id}")
    public Object blog(@PathVariable("id") Integer id){
        Optional<MySQLBlog> blog = mySQLBlogRepository.findById(id);
        MySQLBlog mySQLBlog = blog.get();
        return mySQLBlog;
    }

    @Data
    public static class Param{
        // mysql es
        private String type;
        private String keyword;
    }
}
